package com.bos.mapper;

import org.apache.ibatis.jdbc.SQL;


public class ProjectProvider {
    // 这是一个操作多条件查询的类
    public String findStudent(final String where, final String[] status, final String[] wtype, final String[] pteam, final String[] pleader) {
        String sql = new SQL() {
            {
                // 什么也不输入的话查询出所有的数据
                SELECT("*");
                FROM("bos_Project");
                WHERE("1=1");
                if (where != null && !where.trim().equals("")) {
                    // 如果输入的数字话
                    if (where.matches("[0-9]*")) {
                        AND().WHERE("id = " + where);
                        OR().WHERE("wType like '%" + where + "%'");
                    } else {
                        WHERE("wType like '%" + where + "%'");
                    }
                    OR().WHERE("wName like '%" + where + "%'");
                    OR().WHERE("sTime like '" + where + "'");
                    OR().WHERE("eTime like '" + where + "'");
                    OR().WHERE("pdetail like '%" + where + "%'");
                    OR().WHERE("leader like '%" + where + "%'");
                    OR().WHERE("groups like '%" + where + "%'");
                    OR().WHERE("checker like '%" + where + "%'");
                    OR().WHERE("kehuer like '%" + where + "%'");
                    OR().WHERE("others1 like '%" + where + "%'");
                    OR().WHERE("others2 like '%" + where + "%'");
                    OR().WHERE("remarks like '%" + where + "%'");
                }
                if (null != status && 0 < status.length) {
                    String str = "";
                    if (status.length == 1) {
                        str = "('" + status[0] + "')";
                    } else {
                        for (int i = 0; i < status.length - 1; i++) {
                            if (0 == i) {
                                str = "('" + status[0] + "',";
                            } else {
                                str += "'" + status[i] + "',";
                            }
                        }
                        str += "'" + status[status.length - 1] + "')";
                    }
                    if (where != null && !where.trim().equals("")) {
                        OR().WHERE("progess in " + str);
                    } else {
                        WHERE("progess in " + str);
                    }
                }

                if (wtype != null && wtype.length > 0) {
                    String str = "(";
                    for (int i = 0; i < wtype.length; i++) {
                        if (i == wtype.length - 1) {
                            str += "'" + wtype[i] + "'";
                        } else {
                            str += "'" + wtype[i] + "',";
                        }
                    }
                    str += " )";
                    if ((where != null && !where.trim().equals("")) || (status != null && status.length > 0)) {
                        OR().WHERE(" wType in " + str);
                    } else {
                        WHERE(" wType in " + str);
                    }
                }

                if (pteam != null && pteam.length > 0) {
                    String str = "(";
                    for (int i = 0; i < pteam.length; i++) {
                        if (i == pteam.length - 1) {
                            str += "'" + pteam[i] + "'";
                        } else {
                            str += "'" + pteam[i] + "',";
                        }
                    }
                    str += " )";
                    if (where != null && !where.trim().equals("") && status != null && status.length > 0 && wtype != null && wtype.length > 0) {
                        OR().WHERE(" groups in " + str);
                    } else {
                        WHERE(" groups in " + str);
                    }
                }

                if (pleader != null && pleader.length > 0 && pleader[0] != "") {
                    String str = "(";
                    for (int i = 0; i < pleader.length; i++) {
                        if (i == pleader.length - 1) {
                            str += "'" + pleader[i] + "'";
                        } else {
                            str += "'" + pleader[i] + "',";
                        }
                    }
                    str += " )";
                    if (where != null && !where.trim().equals("") && status != null && status.length > 0 && wtype != null && wtype.length > 0 && pteam != null && pteam.length > 0) {
                        OR().WHERE(" leader in " + str);
                    } else {
                        WHERE(" leader in " + str);
                    }
                }
            }
        }.toString();
        System.out.println(sql);
        return sql;
    }
}
